package com.atnjupt.web;
/**
 * @author Wizarder
 * @create 2021-06-04 12:10
 */

import com.atnjupt.bean.User;
import com.atnjupt.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;

/**代码优化一：
 * 把用户功能整合到一个servlet程序中
 */
public class UserServlet extends HttpServlet {
    private UserServiceImpl usi = new UserServiceImpl();
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //验证请求参数
        User loginUser = usi.login(new User(1, username, password, null));
        if(loginUser == null){
            //1失败跳转当前页面
            //用户或者密码错误，信息回显示客户端
            request.setAttribute("msg","用户或者密码错误");
            //保存客户端输入的内容
            request.setAttribute("username",username);
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request,response);
        }else{
            //2成功跳转登陆成功页面
            request.getRequestDispatcher("/pages/user/login_success.jsp").forward(request,response);
        }
    }
    protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String code = request.getParameter("code");
        //验证验证码-这里验证码写死==abcd
        if("abcd".equalsIgnoreCase(code)){
            //验证码正确
            //验证用户名是否存在
            boolean isExist = usi.existUsername(username);
//            System.out.println(isExist);
            if(isExist){
                //用户名已存在信息传回客户端
                request.setAttribute("msg","用户名已存在");
                request.setAttribute("password",password);
                request.setAttribute("email",email);
                request.setAttribute("code",code);
                System.out.println("用户名存在");
                request.getRequestDispatcher("/pages/user/regist.jsp").forward(request,response);
            }else{
                //保存到数据库中，用户注册成功
                usi.registUser(new User(1,username,password,email));
                //跳转注册成功页面
                request.getRequestDispatcher("/pages/user/regist_success.jsp").forward(request,response);
            }

        }else{
            //验证码不同停留此界面
            //验证码输入错误信息回传客户端
            request.setAttribute("msg","验证码输入有误！");
            //保存用户输入的信息
            request.setAttribute("username",username);
            request.setAttribute("password",password);
            request.setAttribute("email",email);
            System.out.println("验证码[" + code +"]不正确");
            request.getRequestDispatcher("/pages/user/regist.jsp").forward(request,response);
        }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取用户页面的功能的隐藏域表单的value值,实现注册和登陆功能整合
        String action = request.getParameter("action");
        if ("login".equals(action)){
         login(request,response);
        }else if ("regist".equals(action)){
           regist(request,response);
        }
    }
}
